package com.su02.multi.examples.mlp;

import lombok.Getter;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class TrainMatrix4MLP implements Iterable<TrainEntry4MLP> {
    @Getter
    private final List<TrainEntry4MLP> entries;

    private int nCol = -1;

    public TrainMatrix4MLP(List<TrainEntry4MLP> entries) {
        assertSzConsistent(entries);
        this.entries = entries;
    }

    public Pair<Integer, Integer> shape() {
        return Pair.ofNonNull(CollectionUtils.size(entries), nCol);
    }

    private void assertSzConsistent(Collection<TrainEntry4MLP> entries) {
        if (CollectionUtils.isEmpty(entries)) {
            throw new IllegalArgumentException();
        }
        for (TrainEntry4MLP entry : entries) {
            int entrySz = entry.size();
            if (nCol == -1) {
                nCol = entrySz;
            } else if (!Objects.equals(entrySz, nCol)) {
                throw new IllegalArgumentException();
            }
        }
        // should never happen
        if (Objects.equals(nCol, -1)) {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public Iterator<TrainEntry4MLP> iterator() {
        return entries.iterator();
    }
}